// pages/youhui/youhui.js
var app = getApp()
var admin_id = app.globalData.admin_id;
var cat_id = 0;
var share = app.globalData.share;
var left = 250;
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    curIndex: 0,
    isnull: false,
    items: []
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/Api/coupon?admin_id=' + admin_id,
      success: function (e) {
        console.log(e)
        that.setData({
          items: e.data
        })
        that.fomat()
        that.setData({
          all: that.data.items
        })
        that.check()
      }
    })
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  fomat: function () {
    if (!this.data.items) {
      return false
    }
    var arr = this.data.items;
    for (var i = 0; i < arr.length; i++) {
      arr[i].use_start_time = arr[i].use_start_time.split(' ')[0]
      arr[i].use_end_time = arr[i].use_end_time.split(' ')[0]
    }
    this.setData({
      items: arr
    })
  },
  check: function () {

    if (this.data.items.length <= 0) {
      this.setData({
        isnull: true
      })
    } else {
      this.setData({
        isnull: false
      })
    }
  },
  getcard: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/Api/coupon/get?admin_id=' + admin_id + '&cid=' + e.currentTarget.dataset.id + '&openid=' + app.globalData.openid,
      success: function (e) {
        console.log(e)
        wx.showToast({
          title: e.data.msg,
        })
      }
    })
  },
  click: function (e) {
    var that = this;
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        curIndex: e.currentTarget.dataset.index,
        items: that.data.all
      })
      that.check()
    } else {
      wx.request({
        url: 'https://xcxu.we-fs.com/index.php/Api/coupon/lists?admin_id=' + admin_id + '&openid=' + app.globalData.openid,
        success: function (re) {
          console.log(re)
          that.setData({
            items: re.data,
            curIndex: e.currentTarget.dataset.index,
          })
          that.fomat()
          that.check()
        }
      })
    }
  },
  use: function () {
    wx.switchTab({
      url: '../../pages/about/about',
    })
  }
})